﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using AncientLand.Service.Entity;
using AncientLand.Service.IBiz;

namespace AncientLand.Controllers
{
   public class MessageController:  Controller
    {

      private readonly IMessagesBiz _messageService;

      public MessageController(IMessagesBiz messageService)
      {
            this._messageService = messageService;
      }

      public ActionResult ContactUs()
       {
           return View();
       }

       [HttpPost]
      public RedirectToRouteResult ContactUs(Messages itme)
      {
           itme.CreatedTime = DateTime.Now;
           _messageService.Create(itme);
           return RedirectToAction("Suggestions", "Message");
      }


       public ActionResult Suggestions(int? page=1)
       {
            var size =Convert.ToInt16(System.Configuration.ConfigurationSettings.AppSettings["pageSize"]);

            var totalRecord = 0;
            var news = _messageService.GetItemsByPages(out totalRecord,pageIndex:page.GetValueOrDefault(),pageSize:size,orderFiled:"");

            var item = new PageItem()
                           {
                               CurrentPage = page.GetValueOrDefault(),
                               TotalRecords = totalRecord,
                               TotalPages= (totalRecord % size == 0) ? totalRecord / size : totalRecord / size + 1,
                               ControllerName = "Message",
                               ActionName = "Suggestions"
                           };

            var viewModels = new Pager<Messages>() {DataSource = news, Item = item};

            return View(viewModels);
       }
    }
}
